<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\ArticleCat;
use App\Models\ArticleCatAdmin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ArticleController extends BaseController
{
    private $states = [
        'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
        'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
    ];


    public function __construct()
    {
    }

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('文章管理')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('文章详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid           = new Grid(new Article());
        $articleTypeRes = ArticleCat::where('status', 1)->pluck('name as text', 'id')->toArray();

        $grid->filter(function ($filter) use ($articleTypeRes) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->where(function ($query) {
                    $query->where('id', '=', "{$this->input}");
                }, 'ID');
            });

            $filter->column(1 / 2, function ($filter) use ($articleTypeRes) {
                $filter->equal('article_cat_id', '所属栏目')->select($articleTypeRes);
            });
        });

        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', 'ID')->sortable();
        $grid->column('articleCat.name', '所属分类');
        $grid->column('title', '文章标题');
        $grid->column('created_at', '创建时间');
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Article::findOrFail($id));
        $show->article_id('ID');
        $show->articleCat('所属分类', function ($articleType) {
            $articleType->setResource('/admin/article_cat');
            $articleType->name('所属分类');
        });
        $show->title('标题');
        $show->description('文章摘要');
        $show->thumb('文章图片')->image('', 30, 30);
        $show->link('跳转链接');
        $show->content('文章内容')->unescape()->as(function ($content) {
            return $content;
        });
        $show->tag('keywords')->label();
        $show->publish_time('发布时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Article);
        $form->select('article_cat_id', '所属分类')
            ->options(ArticleCatAdmin::selectOptions())->required();
        $form->text('title', '文章标题')
            ->rules('required|max:50', [
                'required' => '文章标题必填',
                'max'      => '文章标题最长50个字符'
            ]);
        $form->image('image', '大图、封面图')->uniqueName();
        $form->editor('content', '内容');
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
